import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
注意：子菜单仅在路由子项时出现。长度>=1详细信息请参见：https:panjiachen。github。iovue元素管理站点指南基本路由器和导航。
 html hidden:true如果设置为true，项目将不会显示在侧边栏中（默认为false）alwaysShow:true如果设置为true，
 将始终显示根菜单如果未设置alwaysShow，当项目有多个子路由时，它将变为嵌套模式，否则不显示根菜单重定向：
 noRedirect如果设置noRedirect将不会在面包屑名称中重定向：'router-name'该名称由<keep alive>使用（必须设置！！！）
 meta:{roles:['admin'，'editor']控制页面角色（您可以设置多个角色）title:'title'侧边栏和面包屑中显示的名称（推荐设置）
 图标：'svg name''el-icon-x'侧边栏面包屑中显示的图标：false如果设置为false，
 该项将隐藏在面包屑中（默认为true）activeMenu:'examplelist'如果设置了路径，侧边栏将突出显示您设置的路径}
 */

/**
 * constantRoutes没有权限要求的基本页所有角色都可以访问
 */
export const constantRoutes = [
  {
    path: '/redirect',//用于快捷标签刷新
    component: Layout,
    hidden: true,
    children: [
      { path: '/redirect/:path(.*)', component: () => import('@/views/redirect/index') }
    ]
  },

  {
    path: '/login',
    component: () => import('@/views/Login'),
    hidden: true,
    meta: { title: '登录'}
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
    meta: { title: '404'}
  },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      component: () => import('@/views/home/index'),
      meta: { title: '信息', icon: 'dashboard', affix: true }
    }]
  },

  {
    path: '/admin',
    component: Layout,
    children: [{
      path: '',
      name: 'Admin',
      component: () => import('@/views/admin/index'),
      meta: { title: '主页', icon: 'el-icon-s-home' }
    }]
  },

  {
    path: '/test',
    hidden: true,
    component: Layout,
    children: [{
      path: '',
      name: 'test',
      component: () => import('@/views/test'),
      meta: { title: '测试', icon: 'el-icon-lollipop' }
    }]
  },

]

/**
 * asyncRoutes需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [

  /* 用户类 */
  {
    path: '/user',
    component: Layout,
    redirect: '/userList',
    name: 'User',
    meta: { title: '用户', icon: 'el-icon-user-solid', roles: '[super,admin]'},
    children: [
      {
        path: '/userList',
        name: 'UserList',
        component: () => import('@/views/user/userList'),
        meta: { title: '用户列表', icon: 'el-icon-document' }
      },
      {
        path: '/adminList',
        name: 'adminList',
        component: () => import('@/views/user/adminList'),
        meta: { title: '管理员列表', icon: 'el-icon-star-on', roles: '[super]'}
      },
    ]
  },

  /* 产品类 */
  {
    path: '/product',
    component: Layout,
    redirect: '/product/list',
    name: 'Product',
    meta: { title: '产品', icon: 'el-icon-shopping-bag-1' },
    children: [
      {
        path: '/product/brand',
        name: 'brand',
        component: () => import('@/views/brand/brand'),
        meta: { title: '品牌管理', icon: 'el-icon-menu' }
      },
      {
        path: '/product/list',
        name: 'productList',
        component: () => import('@/views/product/productList'),
        meta: { title: '产品列表', icon: 'el-icon-s-order' },
      },
      {
        path: '/product/detail/:productId',
        name: 'productDetail',
        hidden: true,
        component: () => import('@/views/product/productDetail'),
        meta: { title: '产品详情', icon: 'el-icon-s-order' }
      },

      {
        path: '/product/add',
        name: 'productAdd',
        component: () => import('@/views/product/productAdd'),
        meta: { title: '添加产品', icon: 'el-icon-document-add' }
      },
    ]
  },

  /* 订单类 */
  {
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', icon: 'el-icon-s-order', roles: '[super,admin]'},
    children: [
      {
        path: '',
        name: 'order',
        component: () => import('@/views/order/order'),
        meta: { title: '订单管理', icon: 'el-icon-s-order' }
      },
      {
        path: '/order/orderDetail/:orderId',
        name: 'orderDetail',
        hidden: true,
        component: () => import('@/views/order/orderDetail'),
        meta: { title: '订单详情', icon: 'el-icon-s-order' }
      },
      {
        path: '/order/userOrder/:userId',
        name: 'userOrder',
        hidden: true,
        component: () => import('@/views/order/userOrder'),
        meta: { title: '用户订单', icon: 'el-icon-s-order' }
      },
      {
        path: '/order/userOrderDetail/:userId/:orderId',
        name: 'userOrderDetail',
        hidden: true,
        component: () => import('@/views/order/userOrderDetail'),
        meta: { title: '订单详情', icon: 'el-icon-s-order' }
      },
      {
        path: '/cart/:userId',
        name: 'ShoppingCart',
        hidden: true,
        component: () => import('@/views/shoppingCart/ShoppingCart'),
        meta: { title: '购物车', icon: 'el-icon-s-order' }
      },
    ]
  },

  /* 设置类 */
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting',
    name: 'Setting',
    meta: { title: '设置', icon: 'el-icon-s-tools', roles: 'super'},
    children: [
      {
        path: '/setting/front',
        name: 'settingFront',
        component: () => import('@/views/user/userList'),
        meta: { title: '前端前台', icon: 'el-icon-s-grid' }
      },
      {
        path: '/setting/admin',
        name: 'settingAdmin',
        component: () => import('@/views/user/userList'),
        meta: { title: '前端后台', icon: 'el-icon-s-operation' }
      },
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const scrollBehavior = function (to, from, savedPosition) {
  // savedPosition 会在你使用浏览器前进或后退按钮时候生效
  // 这个跟你使用 router.go() 或 router.back() 效果一致
  // 这也是为什么我在 tab 栏结构中放入了一个 点击回退 的按钮
  if (savedPosition) {
    return savedPosition
  } else {
    // 如果不是通过上述行为切换组件，就会让页面回到顶部
    return {x: 0, y: 0}
  }
}

const createRouter = () => new Router({
  mode: 'history', // 需要服务支持
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
